package com.test.uiapp2.function.ThirdPartyApplication;

import android.provider.Contacts;
import android.util.Log;

import androidx.test.uiautomator.UiObject;
import androidx.test.uiautomator.UiObjectNotFoundException;
import androidx.test.uiautomator.UiSelector;

import com.test.uiapp2.base.TestBase;
import com.test.uiapp2.element.Base.BaseElement;
import com.test.uiapp2.element.ThirdPartyApplication.WeChatElement;

public class WeChatFunction extends TestBase {
    //打开微信，发送表情
    public void WeChat_chat(){
        try {
            startApp(WeChatElement.WECHATNAME);
            sleep(WAITTIME*2);
            UiObject automatedtest = getTest(WeChatElement.AUTOMATEDTEST);
            if(automatedtest.exists()){
                sleep(WAIT);
                automatedtest.click();
                SendEmoticons();
            }else{
                verify("没有找到自动化测试好友",automatedtest.exists());
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    //打开微信聊天-发送图片
    public void WeChat_picture(){
        try {
            startApp(WeChatElement.WECHATNAME);
            sleep(WAITTIME*2);
            UiObject automatedtest = getTest(WeChatElement.AUTOMATEDTEST);
            if(automatedtest.exists()){
                sleep(WAIT);
                automatedtest.click();
                SendPicture();
            }else{
                verify("没有找到自动化测试好友",automatedtest.exists());
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    //发送图片
    public void SendPicture(){
        try {
            sleep(WAITTIME);
            UiObject expression = getId(BaseElement.WECHAT, WeChatElement.JIA);
            if(expression.exists()){
                expression.click();
                sleep(WAIT);
                UiObject se = getId(BaseElement.WECHAT, WeChatElement.SE);
                UiObject child2 = se.getChild(new UiSelector().className(WeChatElement.LINEARLAYOUT).index(0));
                child2.click();
                sleep(WAITTIME);
                UiObject fbp = getId(BaseElement.WECHAT, WeChatElement.FBP);
                for(int i=0;i<fbp.getChildCount();i++){
                    UiObject child = fbp.getChild(new UiSelector().resourceId(WeChatElement.H9A).index(i));
                    UiObject child1 = child.getChild(new UiSelector().resourceId(WeChatElement.FBE).index(2));
                    child1.click();
                    if(i==3){
                        Log.i(TAG, "选择三张图片，发送");
                        break;
                    }
                }
                sleep(WAITTIME);
                Wechatsend();
                verify("未发送成功",getDescContains(WeChatElement.IMAGE).exists());
                sleep(WAITTIME);


            }else{
                verify("界面异常",expression.exists());
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }


    //发送表情
    public  void  SendEmoticons(){
        try {
            sleep(WAITTIME);
            UiObject expression = getId(BaseElement.WECHAT, WeChatElement.AY9);
            if(expression.exists()){
                expression.click();
                sleep(WAITTIME);
                if(getId(BaseElement.WECHAT,WeChatElement.C0P).exists()){
                    UiObject C0P = getId(BaseElement.WECHAT, WeChatElement.C0P);
                    for (int i=1;i<C0P.getChildCount();i++){
                        Log.i(TAG, "表情总数="+C0P.getChildCount());
                        Log.i(TAG, "当前是第"+i+"表情");
                        UiObject child = C0P.getChild(new UiSelector().className(WeChatElement.FRAMELAYOUT).index(i));
                        child.click();
                        sleep(WAIT);
                        if(i==9){
                            Log.i(TAG, "当前表情为第9个，退出循环");
                            break;
                        }
                    }
                    Wechatsend();
                    sleep(WAIT);
                    UiObject id = getId(BaseElement.WECHAT, WeChatElement.AUJ);
                    UiObject child = id.getChild(new UiSelector().resourceId(WeChatElement.AUJ1).index(0));
                    int length = child.getText().length();
                    Log.i(TAG, "当前输入框length为："+length);
                    verify("未发送成功",length==0);
                    sleep(WAITTIME);
                    swipe_right();
                }
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    public  void  Wechatsend(){
        try {
            sleep(WAIT);
            UiObject send = getTextContains(WeChatElement.SEND);
            if(send.exists()){
                send.click();
            }else{
                verify("没找到发送按钮",send.exists());
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

}
